<template>
  <van-toast id="van-toast" />
</template>

<script setup>
import { loginWechat } from '@/api/login';
import { useUserStore } from '@/store/modules/user/index.js';
import { setToken } from '@/utils/auth';
import Toast from '../../../node_modules/@vant/weapp/dist/toast/toast';
import config from '@/config/config';
import { onLoad } from '@dcloudio/uni-app';
const userStore = useUserStore();


onLoad((e) => {
  console.log(e, '5555555555555555555');
  if (e.token) {
    uni.login({
      "provider": "weixin",
      "onlyAuthorize": true, // 微信登录仅请求授权认证
      success: async (event) => {
        const { code } = event
        const data = {
          code,
          // inviteCode: e.inviteCode
        }
        console.log(data, '微信登陆数据');
        //客户端成功获取授权临时票据（code）,向业务服务器发起登录请求。
        const res = await loginWechat(data)
        console.log(res, '登陆成功数据库');
        setToken(res.data.token)
        userStore.getUserInfo();
        Toast.success('登录成功')
        userStore.updateUserInfo({
          openId: res.data.openid,
          unionId: res.data.unionid
        });
        uni.switchTab({ url: config.page.index })
      }
    })
  }
})
</script>